package datalookin.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.StringCodec;
import com.android.volley.VolleyError;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import datalookin.activity.MainWaterActivity;
import datalookin.activity.MainWaterActivity2;
import datalookin.adapter2.RealPlantAdapter;
import datalookin.adapter2.RealPlantAdapter2;
import datalookin.dto2.WaterPlantDataDto;
import datalookin.network2.GetWaterPlantRealTimeData;
import datalookin.network2.GetWaterPlantRealTimeData2;
import inter.Delegate;

/**
 * 实时厂区数据
 * Created by Administrator on 2016/6/5.
 */
public class RealPlantFragment extends Fragment {

    ListView lv_real;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_realplant,null);
        initview(view);
        return view;
    }

    private void initview(View view) {
        lv_real = (ListView) view.findViewById(R.id.id_lv_historyplant);
        lv_real.setAdapter(realPlantAdapter);
    }

    WaterPlantDataDto waterPlantDataDtoList = new WaterPlantDataDto();
    RealPlantAdapter2 realPlantAdapter = new RealPlantAdapter2();


    GetWaterPlantRealTimeData2 getWaterPlantRealTimeData = new GetWaterPlantRealTimeData2();
    Delegate waterPlantResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
//            waterPlantDataDtoList = JSON.parseObject(response,WaterPlantDataDto.class);
            LinkedHashMap<String,String> map = JSON.parseObject(response,LinkedHashMap.class);
            realPlantAdapter.setWaterPlantDataDtos(map);
            realPlantAdapter.notifyDataSetChanged();

            ((UIHandler.BaseActivity) getActivity()).closeDialog();
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            ((UIHandler.BaseActivity) getActivity()).closeDialog();
        }
    };
    {
      getWaterPlantRealTimeData.setDelegate(waterPlantResponse);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MainWaterActivity2 mainWaterActivity = (MainWaterActivity2) getActivity();
        //        getWaterPlantRealTimeData.setWaterPlantCode(MyApplication.mobileUserDto.getWaterUser().getWaterplantNum());
        getWaterPlantRealTimeData.setWaterPlantCode(mainWaterActivity.getWaterPlantNum());
        getWaterPlantRealTimeData.get();
        ((UIHandler.BaseActivity) getActivity()).showDialog();
    }

    @Override
    public void onStart() {
        super.onStart();

    }
}
